import paramiko
import custom_config


async def save_local(local_path: str, dag_py: str, filename: str):
    local_path += filename
    with open(local_path, 'w') as file:
        # 将字符串写入文件
        file.write(dag_py)

async def save_remote(dag_py: str, filename: str, remote_path: str = custom_config.REMOTE_DAG_PATH, hostname: str = '192.168.110.11', port: int = 22, username: str = 'root', password: str = '123456'):
    # 创建SSH对象
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    # 连接到远程服务器
    ssh.connect(hostname, port, username, password)

    # 使用SFTP客户端
    sftp = ssh.open_sftp()
    # 将字符串写入本地临时文件
    with open(filename, 'w') as file:
        file.write(dag_py)
    # 上传文件到远程服务器

    remote_file = remote_path + filename # 远程完整路径
    # ssh远程上传文件需要先暂存在本地
    local_file = custom_config.LOCAL_DAG_PATH + filename # 本地完整路径
    sftp.put(local_file, remote_file)
    # 关闭连接
    sftp.close()
    ssh.close()